﻿using System;
using System.Collections.Generic;
using System.Text;
using System.Threading;


namespace BasicMachine
{
    public partial class Form1
    {
        private void CheckLimitPlace()
        {
            TimeSpan startTime = new TimeSpan(8, 45, 00);
            while (true)
            {
                foreach (Methods mob in methodOB)
                {
                    if (mob.Limit_Index != 0)
                    {
                        DateTime orderDate = Convert.ToDateTime(mob.OrderPlaceDate);

                        if (orderDate.Date < DateTime.Now.Date && DateTime.Now.TimeOfDay >= startTime)
                        {
                            string OpenCloseAuto = "A";
                            string OrderType = "L";
                            string BuySell;

                            if (mob.Current_Hold > 0)
                                BuySell = "S";
                            else
                                BuySell = "B";


                            PlaceOrder(mob, OpenCloseAuto, BuySell, OrderType, mob.Limit_Index, "N", 'R', false);
                            mob.OrderPlaceDate = DateTime.Now.ToString();
                        }
                    }
                }

                Thread.Sleep(500);
            }
        }

        private void ChangeMonthPlace()
        {
            TimeSpan exchange = new TimeSpan(13, 26, 00);
            DateTime dt = DateTime.Now;
            DateTime tD;
            int Wedcounter = 0;

            for (int i = 1; i <= dt.Day; i++)
            {
                tD = new DateTime(dt.Year, dt.Month, i);

                if (tD.DayOfWeek == DayOfWeek.Wednesday)
                    Wedcounter++;
            }

            while((Wedcounter == 3 && DateTime.Now.DayOfWeek == DayOfWeek.Wednesday) || CBox_NewYear.Checked)
            {
                if (Chk_ChangeMonth.Checked && DateTime.Now.TimeOfDay > exchange)
                {
                    foreach (Methods mob in methodOB)
                    {
                        if (mob.Current_Hold > 0 && mob.excuteMonth == Convert.ToInt32(lab_Month.Text) && lab_Month.Text != lab_NextMonth.Text)
                        {
                            PlaceOrder(mob, "C", "S", "M", 0, "N", 'F', true);
                            mob.OrderPlaceDate = DateTime.Now.ToString();
                            mob.excuteMonth = Convert.ToInt32(lab_NextMonth.Text);
                            saveToXML();
                        }
                        else if (mob.Current_Hold < 0 && mob.excuteMonth == Convert.ToInt32(lab_Month.Text) && lab_Month.Text != lab_NextMonth.Text)
                        {
                            PlaceOrder(mob, "C", "B", "M", 0, "N", 'F', true);
                            mob.excuteMonth = Convert.ToInt32(lab_NextMonth.Text);
                            mob.OrderPlaceDate = DateTime.Now.ToString();
                            saveToXML();
                        }
                    }

                    break;
                }

                Thread.Sleep(666);
            }
        }
    }
}
